CHAPTER 6

Inheritance, Polymorphism, and Encapsulation

Inheritance, polymorphism, and encapsulation comprise the three central characteristics of object-oriented (OO) programming. Inheritance allows you to create class hierarchies, where a base class gives its behavior and attributes to a derived class. You are then free to modify or extend its functionality. Polymorphism ensures that the proper method will be executed based on the calling object’s type. Encapsulation allows you to control access to your object’s state, while making it easier to maintain or change your implementation at a later date.

Inheritance

Inheritance and polymorphism are often considered the backbone of OO programming. Inheritance, like many powerful tools, can be dangerous when misused. The goal of this section is to introduce you to inheritance in a way that makes you respect its power and that helps you to not abuse it.

Earlier, we covered the syntax for defining a class. You specify the base class via the Inherits keyword in the line that follows the class name. In .NET, a class can have only one base class.

Accessibility of Members

Accessibility of members plays an important role in inheritance, specifically with respect to accessing members of the base class from the derived class. Any Publicmembers of the base class become Public members of the derived class.

Any members marked as Protected are only accessible internally to the declaring class and to the classes that inherit from it. Protected members are never accessible publicly from outside the defining class or any class deriving from the defining class. Private members are never accessible to anyone except the defining class. So, even though a derived class inherits all the members of the base class, including the private ones, the code in the derived class cannot access the private members inherited from the base class. In addition, Protected Friend members are visible to all types that are defined within the containing assembly and to classes that derive from the class defining the member. The reality is, the derived class inherits every member of a base class, except instance constructors, the Shared constructor, and Finalize().

As you’ve seen, you can control the accessibility of the entire class itself when you define it. The only possibilities for the class type’s accessibility are Friendand Public. When using inheritance, the rule is that the base class type must be at least as accessible as the deriving class. Consider the following code:

Class A
    Protected x As Integer
End Class

Public Class B
    Inherits A
End Class

The previous code doesn’t compile because Class A is Friend and is not at least as accessible as the deriving Class B. Remember that, in the absence of an access modifier, class definitions default to Friend access—hence, the reason Class A is Friend. In order for the code to compile, you must either promote Class A to Public access or demote Class B to Friend access. Also note that it is legal for Class A to be Public and Class B to be Friend.

Implicit Conversion and a Taste of Polymorphism

You can view inheritance and what it does for you in several ways. First and most obvious, inheritance allows you to borrow an implementation. In other words, you can inherit Class D from Class A and reuse the implementation of Class Ain Class D. It potentially saves you from having to do some work when defining Class D. Another use of inheritance is specialization, where Class D becomes a specialized form of Class A.